package wicket.example.pages;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.extensions.ajax.markup.html.repeater.data.sort.AjaxFallbackOrderByBorder;
import org.apache.wicket.markup.html.TransparentWebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.model.PropertyModel;

import wicket.example.beans.PatientBean;
import wicket.example.providers.PatientBeanDataProvider;
import wicket.example.tableutils.OccupancyColumn;
import wicket.example.tableutils.OccupancyColumnVisibilityTemlate;

public class ExpandableDataTablePage extends WebPage {

	private static final long serialVersionUID = 1L;

	private static final Map<OccupancyColumn, Boolean> DEFAULT_TEMPLETE_MAP = new HashMap<OccupancyColumn, Boolean>();
	static {
		if (DEFAULT_TEMPLETE_MAP.size() == 0) {
			DEFAULT_TEMPLETE_MAP.put(OccupancyColumn.PATIENT_NAME, true);
			DEFAULT_TEMPLETE_MAP.put(OccupancyColumn.ADMISSION, true);
			DEFAULT_TEMPLETE_MAP.put(OccupancyColumn.ALLERGY, true);
		}
	}
	private static final OccupancyColumnVisibilityTemlate DEFAULT_TEMPLETE = new OccupancyColumnVisibilityTemlate(
			"Default", DEFAULT_TEMPLETE_MAP);

	private static final Map<OccupancyColumn, Boolean> DEFAULT_TEMPLETE_MAP_2 = new HashMap<OccupancyColumn, Boolean>();
	static {
		if (DEFAULT_TEMPLETE_MAP_2.size() == 0) {
			DEFAULT_TEMPLETE_MAP_2.put(OccupancyColumn.PATIENT_NAME, true);
			DEFAULT_TEMPLETE_MAP_2.put(OccupancyColumn.ADMISSION, false);
			DEFAULT_TEMPLETE_MAP_2.put(OccupancyColumn.ALLERGY, true);
		}
	}
	private static final OccupancyColumnVisibilityTemlate DEFAULT_TEMPLETE_2 = new OccupancyColumnVisibilityTemlate(
			"Test templete", DEFAULT_TEMPLETE_MAP_2);

	private OccupancyColumnVisibilityTemlate currentTemplete = DEFAULT_TEMPLETE;

	public ExpandableDataTablePage() {

		PatientBeanDataProvider dataProvider = new PatientBeanDataProvider();
		DataView<PatientBean> view = new DataView<PatientBean>("table",
				dataProvider, 20) {

			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(Item<PatientBean> item) {
				final PatientBean modelObject = item.getModelObject();
				Label name = new Label("name", new PropertyModel<String>(
						modelObject, "name")) {
					private static final long serialVersionUID = 1L;

					@Override
					public boolean isVisible() {
						return isColumnVisisble(currentTemplete,
								OccupancyColumn.PATIENT_NAME);
					}
				};
				item.add(name);
				Label surname = new Label("surname", new PropertyModel<String>(
						modelObject, "surname")) {
					private static final long serialVersionUID = 1L;

					@Override
					public boolean isVisible() {
						return isColumnVisisble(currentTemplete,
								OccupancyColumn.ADMISSION);
					}
				};
				item.add(surname);
				Label age = new Label("age", new PropertyModel<String>(
						modelObject, "age")) {
					private static final long serialVersionUID = 1L;

					@Override
					public boolean isVisible() {
						return isColumnVisisble(currentTemplete,
								OccupancyColumn.ALLERGY);
					}
				};
				item.add(age);
			}
		};
		final TransparentWebMarkupContainer testContainer = new TransparentWebMarkupContainer(
				"tableWrapper");
		testContainer.setOutputMarkupId(true);
		testContainer.add(view);
		this.add(testContainer);

		AjaxFallbackOrderByBorder<String> nameSorting = new AjaxFallbackOrderByBorder<String>(
				"name.sorting", "name", dataProvider) {

			private static final long serialVersionUID = 1L;

			@Override
			public boolean isVisible() {
				return isColumnVisisble(currentTemplete,
						OccupancyColumn.PATIENT_NAME);
			}

			@Override
			protected void onAjaxClick(AjaxRequestTarget target) {
				target.add(testContainer);
			}
		};
		testContainer.add(nameSorting);

		AjaxFallbackOrderByBorder<String> surnameSorting = new AjaxFallbackOrderByBorder<String>(
				"surname.sorting", "surname", dataProvider) {

			private static final long serialVersionUID = 1L;

			@Override
			public boolean isVisible() {
				return isColumnVisisble(currentTemplete,
						OccupancyColumn.ADMISSION);
			}

			@Override
			protected void onAjaxClick(AjaxRequestTarget target) {
				target.add(testContainer);
			}
		};

		testContainer.add(surnameSorting);

		AjaxFallbackOrderByBorder<String> ageSorting = new AjaxFallbackOrderByBorder<String>(
				"age.sorting", "age", dataProvider) {

			private static final long serialVersionUID = 1L;

			@Override
			public boolean isVisible() {
				return isColumnVisisble(currentTemplete,
						OccupancyColumn.ALLERGY);
			}

			@Override
			protected void onAjaxClick(AjaxRequestTarget target) {
				target.add(testContainer);
			}
		};
		testContainer.add(ageSorting);

		this.add(new PagingNavigator("navigator", view));

		IChoiceRenderer<OccupancyColumnVisibilityTemlate> renderer = new ChoiceRenderer<OccupancyColumnVisibilityTemlate>(
				"templeteName");
		DropDownChoice<OccupancyColumnVisibilityTemlate> templeteDropDown = new DropDownChoice<OccupancyColumnVisibilityTemlate>(
				"templeteDropDown",
				new PropertyModel<OccupancyColumnVisibilityTemlate>(this,
						"currentTemplete"), Arrays.asList(DEFAULT_TEMPLETE,
						DEFAULT_TEMPLETE_2), renderer);
		templeteDropDown.setNullValid(false);
		templeteDropDown.add(new AjaxFormComponentUpdatingBehavior("onchange") {

			private static final long serialVersionUID = 1L;

			@Override
			protected void onUpdate(AjaxRequestTarget target) {
				target.add(testContainer);
			}
		});
		Form<Void> templeteDropDownForm = new Form<Void>("templeteDropDownForm");
		templeteDropDownForm.add(templeteDropDown);
		this.add(templeteDropDownForm);
	}

	public boolean isColumnVisisble(OccupancyColumnVisibilityTemlate template,
			OccupancyColumn column) {
		return template.isVisible(column);
	}

}
